package tech.abing.spider.task.crawler.amac;

import java.util.Arrays;
import java.util.List;

import tech.abing.spider.CrawTaskBuilder;
import tech.abing.spider.CrawlManager;
import tech.abing.spider.CrawlTask;
import tech.abing.spider.component.DynamicEntrance;
import tech.abing.spider.component.listener.TaskLifeListener;
import tech.abing.spider.downloader.impl.DefaultPageDownloader;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.PageRequest.Method;
import tech.abing.spider.request.PageRequest.PageEncoding;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.constants.DictEnum;
import tech.abing.spider.task.pageprocessor.amac.CustodianProcessor;
import tech.abing.spider.task.pipeline.amac.CustodianPipeline;
import tech.abing.spider.util.SqlUtil;

public class CustodianSpider {

	private static SqlUtil sqlUtil = null;
	
	public static void start() {
		CrawlManager custodian = CrawlManager.getInstance();
		
		CrawTaskBuilder builder = custodian
		.prepareCrawlTask(DictEnum.TAKS_TYPE_CUSTODIAN.getName(), DefaultPageDownloader.class)
		.useThread(20)
		.useDynamicEntrance(DynamicEntranceImpl.class)
		.usePageEncoding(PageEncoding.UTF8)
		.usePageRetryCount(3)
		.setPolitenessDelay(200)
		.usePipeline(CustodianPipeline.class)
		.useTaskLifeListener(new TaskLifeListener(){

			@Override
			public void onStart(CrawlTask task) {
				System.out.println("================开始");
				if(sqlUtil==null){
					sqlUtil = SqlUtil.getInstance();
				}
				sqlUtil.deleteForConn("delete from "+DictEnum.TAKS_TYPE_CUSTODIAN.getCode());
			}

			@Override
			public void onFinished(CrawlTask task) {
//				task.getPipeline().destory();
				sqlUtil.closeConn();
				sqlUtil = null;
				System.out.println("================完成");
			}
			
		});
		
		CrawlTask task = builder.build();
		
		custodian.start(task);
		
	}
	
	public static final class DynamicEntranceImpl extends DynamicEntrance{

		@Override
		public List<StartContext> loadStartContext() {
			StartContext context = new StartContext();
			String url = "http://www.f5.amac.org.cn/xxgs/cyjggs/jjtgx/382722.shtml";
			PageRequest req = context.createPageRequest(url, CustodianProcessor.class);
			
			req.setMethod(Method.GET);
			
			req.putHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
			req.putHeader("Accept-Encoding","gzip, deflate, sdch");
			req.putHeader("Accept-Language","zh-CN,zh;q=0.8");
			req.putHeader("Cache-Control","max-age=0");
			req.putHeader("Connection","keep-alive");
			req.putHeader("Cookie","22_vq=11");
			req.putHeader("Host","www.f5.amac.org.cn");
			req.putHeader("Upgrade-Insecure-Requests","1");
			req.putHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");
			
			context.injectSeed(req);
			return Arrays.asList(context);
		}
		
	}
	
}
